package net.huashitong.Indicators.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.CompletionClassificationDatabean;
import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.CompletionClassification;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.ComparatorUtils;
import net.huashitong.Indicators.utils.Comparators;
import net.huashitong.Indicators.utils.ComparatorsUtils;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
@Service
public class ConstructionIndustryService {
	@Autowired
	private TertiaryIndustryService industryService;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Autowired
	private CompletionClassificationService classificationService;
	
	public IndicatorBreakdownDatabean queryCountyRank(
			IndicatorBreakdownFilter filter) {
		List<IndicatorBreakdownDatabean> databeans = new ArrayList<IndicatorBreakdownDatabean>(13);
		List<IndicatorBreakdown> lists = breakdownService.queryCountyRank(filter);
		if(lists.size()<3){
			return null;
		}
		IndicatorBreakdownDatabean databeans2 = new IndicatorBreakdownDatabean();
		DecimalFormat df=new DecimalFormat("##.##");
		int i=0;
		for (IndicatorBreakdown indicatorBreakdown : lists) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setArea(indicatorBreakdown.getArea());
			databean.setRatio(df.format(Double.parseDouble(indicatorBreakdown.getRatio())));
			databean.setCumulative(Double.toString(Double.parseDouble(indicatorBreakdown.getCumulative())/10000).substring(0,3));
			if(indicatorBreakdown.getArea().contains("福州市")){
			}else if(indicatorBreakdown.getArea().contains("永泰")){
				String st=df.format(Double.parseDouble(indicatorBreakdown.getExpectedTarget())/10000);
				databeans2.setExpectedTarget(st);
				databeans2.setArea(indicatorBreakdown.getArea());
				//databeans.add(2, databean);
				databean.setIndex(2);
				databeans.add(databean);
			}else {
				databeans.add(databean);
			}
		}
		databeans2.setRightdata(databeans);
		return databeans2;
	}

	public List<IndicatorBreakdownDatabean> queryDevelopmentSituation(
			IndicatorBreakdownFilter filter) throws ParseException {
		//查找当季和前一个季度数据
				List<IndicatorBreakdownDatabean> breakdownDatabean2s = new ArrayList<IndicatorBreakdownDatabean>();
				List<IndicatorBreakdown> lists = breakdownService.queryDevelopmentSituation(filter);
				//校验
				if(lists.size()<2){
					return null;
				}
				TertiaryIndustry industries = industryService.queryByfilter(filter);
				if(ObjectUtils.isBlank(industries)){
					return null;
				}
				IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
				String lastDateVal = "";
				String DateVal = "";
				for (IndicatorBreakdown indicatorBreakdown : lists) {
					if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -3))){
						lastDateVal = indicatorBreakdown.getCumulative();
					}else {
						DateVal = indicatorBreakdown.getCumulative();
						databean.setRatio(indicatorBreakdown.getRatio());
					}
					
				}
				if(StringUtils.isBlank(lastDateVal)){
					lastDateVal="0";
				}
				System.out.println("lastDateVal="+lastDateVal);
				Double double1 = Double.parseDouble(lastDateVal);
				Double double2 = Double.parseDouble(DateVal);
				Double double3 = double2-double1;
				//查找去年当季和去年当季前一个季度数据
				filter.setCurrentMonth(DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(),-12));
				List<IndicatorBreakdown> indicatorBreakdowns = breakdownService.queryDevelopmentSituation(filter);
				//校验
				if(indicatorBreakdowns.size()<2){
					return null;
				}
				for (IndicatorBreakdown indicatorBreakdown : indicatorBreakdowns) {
					if(StringUtils.equals(indicatorBreakdown.getCurrentMonth(), DateForIndUtils.getNextMonthDate(filter.getCurrentMonth(), -3))){
						lastDateVal = indicatorBreakdown.getCumulative();
					}else {
						DateVal = indicatorBreakdown.getCumulative();
					}
				}
				double1 = Double.parseDouble(lastDateVal);
				double2 = Double.parseDouble(DateVal);
				Double double4 = double2-double1;
				double3 =(double3-double4)/double4*100;
				if(double3 != 0){
					DecimalFormat df=new DecimalFormat(".##");
					String st=df.format(double3);
					databean.setConcurrentGrowth(st);
				}
				databean.setCountyIncrease(industries.getCountyIncrease());
				databean.setCityIncrease(industries.getCityIncrease());
				databean.setuBIncrease(industries.getuBIncrease());
				breakdownDatabean2s.add(databean);
				return breakdownDatabean2s;
	}

	public List<Map<String, Object>> queryAbsoluteValue(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		List<IndicatorBreakdownDatabean> breakdownDatabeans2 = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans3 = new ArrayList<IndicatorBreakdownDatabean>();
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		breakdownDatabeans = DataUtils.detailData(breakdownSs,breakdownDatabeans);
		for (IndicatorBreakdownDatabean breakdownDatabean : breakdownDatabeans) {
			String date = breakdownDatabean.getCurrentMonth();
			for (IndicatorBreakdownDatabean databean : breakdownDatabeans) {
				if(databean.getCurrentMonth().contains(DateForIndUtils.getNextMonthDate(date,-12))){
					Double double5 = Double.parseDouble(breakdownDatabean.getSeasonsComparison());
					Double double6 = Double.parseDouble(databean.getSeasonsComparison());
					Double double7 = (double5-double6)/double6*100;
					if(double7 != 0){
						DecimalFormat df=new DecimalFormat(".##");
						String st=df.format(double7);
						breakdownDatabean.setConcurrentGrowth(st);
					}
				}
			}
			breakdownDatabean.setSeasonsComparison(breakdownDatabean.getSeasonsComparison().substring(0, 2));
			if(breakdownDatabean.getCurrentMonth().contains("2016")){
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans2.add(breakdownDatabean);
			}else {
				breakdownDatabean.setCurrentMonth(breakdownDatabean.getCurrentMonth().substring(4));
				breakdownDatabeans3.add(breakdownDatabean);
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabeans2);
		Collections.reverse(breakdownDatabeans3);
		map2.put("lastYear", breakdownDatabeans2);
		map2.put("years", breakdownDatabeans3);
		list.add(map2);
		return list;
	}

	public List<Map<String, Object>> queryCumulativeGrowth(
			IndicatorBreakdownFilter filter) throws ParseException {
		IndicatorBreakdown breakdown = breakdownService.queryMonthValue(filter);
		if(ObjectUtils.isBlank(breakdown)){
			return null;
		}
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate().substring(0,4),0));
		List<IndicatorBreakdown> breakdowns = breakdownService.queryAbsoluteValue(filter);
		List<IndicatorBreakdownDatabean> breakdownDatabeans = new ArrayList<IndicatorBreakdownDatabean>();
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdowns,breakdownDatabeans);
		filter.setDate(DateForIndUtils.getNextYearDate(filter.getDate(),-1));
		List<IndicatorBreakdown> breakdownSs = breakdownService.queryAbsoluteValue(filter);
		DecimalFormat df=new DecimalFormat("#.##");
		breakdownDatabeans = DataUtils.detailcumulativeData(breakdownSs,breakdownDatabeans);
		List<IndicatorBreakdownDatabean> breakdownDatabean4s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdownDatabeans4 = new ArrayList<IndicatorBreakdownDatabean>();
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdownDatabeans) {
			indicatorBreakdown.setCumulative(df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000));
			if(indicatorBreakdown.getCurrentMonth().contains(filter.getDate().substring(0,4))){
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabean4s.add(indicatorBreakdown);
			}else {
				indicatorBreakdown.setCurrentMonth(indicatorBreakdown.getCurrentMonth().substring(4));
				breakdownDatabeans4.add(indicatorBreakdown);
			}
		}
		List<Map<String, Object>> list32 = new ArrayList<Map<String,Object>>();
		Map<String, Object> map32 = new HashMap<String, Object>();
		Collections.reverse(breakdownDatabean4s);
		Collections.reverse(breakdownDatabeans4);
		map32.put("years",breakdownDatabeans4);
		map32.put("lastYear", breakdownDatabean4s);
		String st=df.format(Double.parseDouble(breakdowns.get(0).getExpectedTarget())/10000);
		map32.put("expectedTarget", st);
		list32.add(map32);
		return list32;
	}

	public List<CompletionClassificationDatabean> queryByDWValue(
			IndicatorBreakdownFilter filter) {
		List<CompletionClassification> classificationds = classificationService.queryByDWValue(filter);
		if(ObjectUtils.isBlank(classificationds)){
			return null;
		}
//		filter.setDate(DateForIndUtils.getNextMonthValueDate(filter.getDate(), -12));
//		List<CompletionClassification> classificationd2s = classificationService.queryByDWValue(filter);
		int k = 0;
		List<CompletionClassificationDatabean> classificationDatabeands = new ArrayList<CompletionClassificationDatabean>(); 
		for (int i = filter.getPage()*20; i < classificationds.size(); i++) {
			if(k == 20){
				break;
			}
//			for (CompletionClassification completionClassification2 : classificationd2s) {
//				if(completionClassification.getCategoryDetails().equals(completionClassification2.getCategoryDetails())){
					CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
					DecimalFormat df=new DecimalFormat("#.##");
					classification.setActualCompletionValue(df.format(Double.parseDouble(classificationds.get(i).getActualCompletionValue())/10000));
//					classification.setPlanneValue(classificationds.get(i).getPlanneValue());
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("永泰县", ""));
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("福建省", ""));
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("福州市", ""));
					classificationds.get(i).setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("福州", ""));
					classification.setCategoryDetails(classificationds.get(i).getCategoryDetails().replace("有限公司", ""));
					
					String st = df.format(Double.parseDouble( StringUtils.isBlank(classificationds.get(i).getSpecificIncrease())?"0":classificationds.get(i).getSpecificIncrease()    ));
					classification.setSpecificIncrease(st);
//					classification.setCompleteAnnual(df.format(Double.parseDouble(classificationds.get(i).getCompleteAnnual())));
					if(Double.parseDouble(classificationds.get(i).getActualCompletionValue()) == 0 ){
						classification.setLastActualCompletionValue("0");
					}else {
						classification.setLastActualCompletionValue(df.format(Double.parseDouble(   StringUtils.isBlank(classificationds.get(i).getActualCompletionValue())?"0":classificationds.get(i).getActualCompletionValue()     )/(1+Double.parseDouble( StringUtils.isBlank(classificationds.get(i).getSpecificIncrease())?"0":classificationds.get(i).getSpecificIncrease()    )/100)/10000));
					}
					
					classificationDatabeands.add(classification);
					k++;
//				}
//			}
		}
		return classificationDatabeands;
	}

	public List<CompletionClassificationDatabean> queryByDWValueSec(
			IndicatorBreakdownFilter filter) {
		List<CompletionClassification> classificationds = classificationService.queryByDWValue(filter);
		if(ObjectUtils.isBlank(classificationds)){
			return null;
		}
		List<CompletionClassificationDatabean> classificationDatabeands = new ArrayList<CompletionClassificationDatabean>(); 
		for (CompletionClassification completionClassification : classificationds) {
			CompletionClassificationDatabean classification = new CompletionClassificationDatabean();
			DecimalFormat df=new DecimalFormat(".##");
			String st=df.format(Double.parseDouble(completionClassification.getActualCompletionValue())/100000);
			classification.setActualCompletionValue(st);
			if(StringUtils.isNotBlank(completionClassification.getPlanneValue())){
				st = df.format(Double.parseDouble(completionClassification.getPlanneValue())/100000);
				classification.setPlanneValue(st);
				Double double1 = (Double.parseDouble(completionClassification.getActualCompletionValue())/10000)/(Double.parseDouble(completionClassification.getPlanneValue())/10000)*100;
				st = df.format(double1);
				classification.setCompleteAnnual(st);
			}
			if(StringUtils.isNotBlank(classification.getCompleteAnnual())){
				classification.setCompleteAnnual(df.format(Double.parseDouble(classification.getCompleteAnnual())));
				classification.setLastActualCompletionValue(df.format(Double.parseDouble(completionClassification.getActualCompletionValue())/(1+Double.parseDouble(classification.getCompleteAnnual())/100)));
			}
//			if(double1 < DataUtils.warning(filter)){
//				classification.setYujz("0");
//			}else {
//				classification.setYujz("1");
//			}
			classification.setCategoryDetails(completionClassification.getCategoryDetails().replace("福建省", "").replace("有限公司", "").replace("永泰县", ""));
			classificationDatabeands.add(classification);
		}
//		Collections.sort(classificationDatabeands,new ComparatorsUtils());
		List<CompletionClassificationDatabean> classificationDatabeands2 = new ArrayList<CompletionClassificationDatabean>(); 
		for (int i = 0; i < classificationDatabeands.size(); i++) {
			if(i == 20){
				break;
			}
			classificationDatabeands2.add(classificationDatabeands.get(i));
		}
		Collections.reverse(classificationDatabeands2);
		return classificationDatabeands2;
	}

}
